package com.example.smartretaildemo.module.shoppingcart.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.smartretaildemo.R;
import com.example.smartretaildemo.bean.shoppingcart.ShopCartInfo;
import com.example.smartretaildemo.databinding.ItemRvShopcartBinding;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ShopCartRViewAdapter$
 * @Description: 购物车适配器
 * @Author: letquit
 * @CreateDate: 21/9/2021$
 * @Version: 1.0
 */
public class ShopCartRViewAdapter extends RecyclerView.Adapter<ShopCartRViewAdapter.ViewHolder> {
    private Context context;
    private List<ShopCartInfo> list;
    private static Map<Integer, Boolean> checkStatus = new HashMap<>();
    private Map<Integer, Boolean> map = new HashMap<>();


    public ShopCartRViewAdapter(Context context, List<ShopCartInfo> list) {
        this.context = context;
        this.list = list;
    }

    //    全选
    public void selectAll() {
        initCheck(true);
        notifyDataSetChanged();
    }

    //    全不选
    public void unSelectAll() {
        initCheck(false);
        notifyDataSetChanged();
    }

    //    修改所选
    public void initCheck(boolean flag) {
        for (int i = 0; i < list.size(); i++) {
            checkStatus.put(i, flag);
        }
    }

    //    获取所选CheckBox状态
    public static Map<Integer, Boolean> getCheck() {
        return checkStatus;
    }

    //    将CheckBox状态插入Map
    public static void setCheck(int position, boolean isChecked) {
        checkStatus.put(position, isChecked);;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_shopcart, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder v = (ViewHolder) holder;
        ShopCartInfo shopCartInfo = list.get(position);
        String comName = shopCartInfo.getCommodityName();
        String num = String.valueOf(shopCartInfo.getNum());
        String pic = shopCartInfo.getPic();
        String price = String.format("%1.2f", shopCartInfo.getfCostPrice());
        v.tvShopComName.setText(comName);
        v.tvShopNum.setText(num);
        v.tvShopPrice.setText("单价：￥" + price);
        Glide.with(context).load(pic).into(v.imgShopPic);
        v.cheBoxShop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                setCheck(position, v.cheBoxShop.isChecked());
                if (b) {
                    map.put(position, true);
                    checkStatus.put(position, b);
                } else {
                    map.remove(position);
                }
            }
        });
        if (map != null && map.containsKey(position)) {
            v.cheBoxShop.setChecked(true);
        } else {
            v.cheBoxShop.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ItemRvShopcartBinding binding;
        public CheckBox cheBoxShop;
        ImageView imgShopPic;
        TextView tvShopComName;
        public TextView tvShopPrice, tvShopNum, tvShopSub, tvShopAdd;
        public Button btnShopDel;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = ItemRvShopcartBinding.bind(itemView);
            cheBoxShop = binding.cheBoxShop;
            imgShopPic = binding.imgShopPic;
            tvShopComName = binding.tvShopComName;
            tvShopSub = binding.tvShopSub;
            tvShopNum = binding.tvShopNum;
            tvShopAdd = binding.tvShopAdd;
            tvShopPrice = binding.tvShopPrice;
            btnShopDel = binding.btnShopDel;
        }
    }
}
